import { _decorator, Component, director, instantiate, Node } from 'cc';
import { BilliardsGameEvent, BilliardsGameMgr } from '../scripts/BilliardsGameMgr';
import { BallUVSetter } from '../scripts/BallUVSetter';
import { UserMgr } from '../../module_basic/scripts/UserMgr';
const { ccclass, property } = _decorator;

@ccclass('PocktedBalls')
export class PocktedBalls extends Component {
    @property(Node)
    leftPlayer: Node;

    @property(Node)
    rightPlayer: Node;

    private _prefab: Node;

    start() {
        this._prefab = this.leftPlayer.children[0];
        this.leftPlayer.active = false;
        this.rightPlayer.active = false;
        if (BilliardsGameMgr.inst.isOnline && BilliardsGameMgr.inst.gameData) {
            if (BilliardsGameMgr.inst.leftUserId) {
                this.initPocketedBalls(BilliardsGameMgr.inst.leftUserId, this.leftPlayer, false);
            }
            if (BilliardsGameMgr.inst.rightUserId) {
                this.initPocketedBalls(BilliardsGameMgr.inst.rightUserId, this.rightPlayer, true);
            }
        }
        else {
            this.initPocketedBalls(UserMgr.inst.userId, this.leftPlayer, false);
        }

        director.getScene().on(BilliardsGameEvent.PLAYER_DATA_CHANGED, (msg) => {
            let player = BilliardsGameMgr.inst.getPlayer(msg.uid);
            if (player && player.pocketedBalls) {
                let root = this.leftPlayer;
                let isRight = msg.uid == BilliardsGameMgr.inst.rightUserId;
                if (isRight) {
                    root = this.rightPlayer;
                }
                for (let i = root.children.length; i < player.pocketedBalls.length; ++i) {
                    this.addPocketedBall(player.pocketedBalls[i], root, isRight);
                }
            }
        });
    }

    onPocket(uid: string, ballId: number) {
        if (uid == BilliardsGameMgr.inst.leftUserId || uid == UserMgr.inst.userId) {
            this.addPocketedBall(ballId, this.leftPlayer, false);
        }
        else {
            this.addPocketedBall(ballId, this.rightPlayer, true);
        }
    }

    private initPocketedBalls(uid: string, root: Node, isRight: boolean) {
        root.active = true;
        root.removeAllChildren();
        let player = BilliardsGameMgr.inst.getPlayer(uid);
        if (player && player.pocketedBalls) {
            for (let i = 0; i < player.pocketedBalls.length; ++i) {
                this.addPocketedBall(player.pocketedBalls[i], root, isRight);
            }
        }
    }

    private addPocketedBall(ballId: number, root: Node, isRight: boolean) {
        let size = this._prefab.worldScale.x;
        let gap = size / 10;
        let signX = isRight ? -1 : 1;
        let pocketedBall = instantiate(this._prefab);
        let index = root.children.length;
        root.addChild(pocketedBall);
        pocketedBall.getComponent(BallUVSetter).ballId = ballId;
        pocketedBall.setPosition(signX * (size * index + gap * index), 0, 0);
    }

    update(deltaTime: number) {

    }
}


